//
//  FiltrateViewController.m
//  DuBai
//
//  Created by darktm on 14-2-25.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "FiltrateViewController.h"

#import "DelButton.h"

@interface FiltrateViewController ()<DelButtonDelegate>

@property(strong)NSMutableArray *brandArray;

@end

@implementation FiltrateViewController

@synthesize delegate;

#define TAG_BRAND 10000

@synthesize brandArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.

    
    self.finished.layer.cornerRadius = 13.0f;
    self.finished.layer.masksToBounds = YES;
    
    float orgx = 5.00;
    float dur = 5.0f;
    
    brandArray = [NSMutableArray array];
    
    for (int i = 0; i < 15; i++) {
        
        NSMutableString *s = [NSMutableString stringWithString:@"你"];

        int j = i;
        while (j--) {
            [s appendString:@"好"];
        }
        
        [brandArray addObject:s];
    }
    
    for (int i = 0; i < brandArray.count; i++) {
        DelButton *btn = [[DelButton alloc] initWithFrame:CGRectMake(orgx, 2, 100, 35)];
        btn.tag = TAG_BRAND + i;
        [btn setCutstomerTitle:brandArray[i]];
        btn.delegate = self;
        [self.sv1 addSubview:btn];
        orgx += dur + btn.frame.size.width;
    }
    self.sv1.contentSize = CGSizeMake(orgx, 1);
    
    
}

-(void)didDelBtn:(DelButton *)btn{

    UIScrollView *sv = (UIScrollView *)btn.superview;
    
    sv.userInteractionEnabled = NO;
    
    float wideDur = btn.frame.size.width + 5;
    
    [UIView animateWithDuration:.2 animations:^{
        btn.alpha = 0 ;
    } completion:^(BOOL finished) {
        [btn removeFromSuperview];
    }];

    
    
    UIView *tmp = nil;
    
    for (int i = btn.tag + 1; i < brandArray.count + TAG_BRAND; i++) {
        
        tmp = [sv viewWithTag:i];
        tmp.tag--;
        [UIView animateWithDuration:.2 animations:^{
            CGRect frame = tmp.frame;
            frame.origin.x -= wideDur;
            tmp.frame = frame;
        }];
    
    }
    [brandArray removeObjectAtIndex:btn.tag - TAG_BRAND];
    
    sv.contentSize = CGSizeMake(sv.contentSize.width - wideDur, 1);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        sv.userInteractionEnabled = YES;
    });
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)on_btn_finished:(id)sender {
    
    [delegate didSeletedValues:@"asdfsd"];
    [self.navigationController popViewControllerAnimated:YES];
}
@end
